package com.zl.zsk.config.security;

import com.zl.zsk.constant.RetCode;
import com.zl.zsk.exception.JwtInvalidException;
import com.zl.zsk.pojo.vo.Result;
import com.zl.zsk.util.SecurityResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * @author lxb
 * @version 1.0.0
 * @desc:
 * @date 2020-03-16 10:11
 */
public class JwtAuthEntryPoint implements AuthenticationEntryPoint {
    private static final Logger log = LoggerFactory.getLogger(JwtAuthEntryPoint.class);
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException {
        log.debug("【JwtAuthEntryPoint】：{}",e.getMessage());
        String msg = "认证失败";
        int code = RetCode.AUTH_FAIL.code;
        if(e instanceof JwtInvalidException){
            msg = e.getMessage();
            code = RetCode.JWT_TOKEN_ERROR.code;
        }
        Result result = new Result(null,msg,false,code);
        SecurityResponse.getCommonResponse(response,result);
    }
}
